package com.common.update;

import android.content.Context;
import android.os.Binder;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.ToastHelper;
import com.orangelife.mobile.app.service.MyService;
import com.orangelife.mobile.constants.CommonURL;
import com.orangelife.mobile.individual.activity.SettingActivity;
import com.orangelife.mobile.main.activity.MainTablActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by orangelife on 16/11/8.
 */

public class UpdateBiz {

    /**
     * 请求版本更新
     */
    public static void requestUpdateVersion(final MyService.MyBinder binder, final Context context,final int fromwhere) {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("appType", "1");
        VolleyHttpUtil.getInstance().getWithCallback(CommonURL.VERSION, map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                boolean isSuccess = false;
                if (OrangeErrorHandler.getInstance().isSuccess(jsonObject, false)) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(jsonObject);
                    if (map != null && map.size() > 0) {
                        isSuccess = true;
                        final String newVersion = String.valueOf(map.get("version"));
                        final String minVersion = String.valueOf(map.get("minVersion"));
                        final String url = String.valueOf(map.get("url"));
                        final String versionInfo = String.valueOf(map.get("remark"));
                        final String isForce = String.valueOf(map.get("isForce"));
                        if (binder != null) {
                            binder.check(fromwhere, context, isForce, newVersion, minVersion, url, versionInfo, false);
                        }
                    }
                }
                if (!isSuccess) {
                    ToastHelper.getInstance()._toast("获取版本信息失败");
                }
            }
        });
    }

}
